<?php
$this->template->add_admin_js_lib("SimpleAjaxUploader.min.js");
$game_id = (isset($edit->{$this->{$this->model}->primaryKey})?$edit->{$this->{$this->model}->primaryKey}:0);
?>
<form method="post" action="<?php echo $this->url_edit . $game_id; ?>"  enctype="multipart/form-data" class="form-horizontal">
<div class="widget-box">
	<div class="widget-title">
		<ul class="nav nav-tabs">
			<li class="active"><a data-toggle="tab" href="#tab1">Thông tin cơ bản</a></li>
			<li><a data-toggle="tab" href="#tab2">Game file</a></li>
			<li><a data-toggle="tab" href="#tab3">Hướng dẫn</a></li>
			<li><a data-toggle="tab" href="#tabseo">SEO</a></li>
		</ul>
	</div>
	<div class="widget-content tab-content">
		<div id="tab1" class="tab-pane active"><div class="widget-content nopadding">
				<?php echo $this->atheme->group_text2('Tên game', @$edit->game_title); ?>
				<?php $link = '';
					if($game_id >0){
						$link = game_url($edit->game_id, $edit->game_code);
						$link = '/choi-game/'.$edit->game_code.'-'.$edit->game_id.'.html';
						$link = '<a href="'.$link.'" target="_blank" >'.$link.'</a>';
					}
				?>
				<?php echo $this->atheme->group_text2('Link game', @$link); ?>
				<?php echo $this->atheme->group_select2('cat_id[]','Danh mục','','', $cate_tree,@$game_cate,' multiple'); ?>
				<?php echo $this->atheme->group_text2('Game Key', @$edit->game_key); ?>
				<?php echo $this->atheme->group_text2('Hướng dẫn', @$edit->game_content); ?>
				<?php 
				/*
				if($game_id > 0){
				 echo $this->atheme->group_text2('Game Key',@$edit->game_key); 
					}else {
						echo $this->atheme->group_input2('edit[game_key]', 'Game Key', '', 'span6 ',md5(uniqid(rand(), true)), 'text', 'readonly="readonly"');
					}
					*/
					?>
				<?php echo $this->atheme->group_text2('Lượt chơi', @$edit->game_view); ?>
				
				<?php
				$arr_status = array();
				if($this->mgame->permission('process_game'))
				{
					$arr_status['2'] = 'Duyệt game, được thách đấu';
					$arr_status['1'] = 'Duyệt game, không thách đấu';
					$arr_status['0'] = 'Ẩn';
					$arr_status['-2'] = 'Test lại game';
					$arr_status['-3'] = 'Sửa lại game';
				}
				else if($this->mgame->permission('post_game'))
				{
					$arr_status['-3'] = 'Lưu tạm';
					$arr_status['-2'] = 'Chờ test';
				}
				?>
				
				<?php echo $this->atheme->group_select2('edit[game_status]','Trạng thái','','', $arr_status,@$edit->game_status,'style="min-width: 300px;"'); ?>
				<?php //echo $this->atheme->group_select2('edit[game_status]','Trạng thái','','', array('1'=>'Hiện, không thách đấu','2'=>'Hiện, được thách đấu','0'=>'Ẩn'),@$edit->game_status); ?>
				
				<div class="form-actions">
					<button type="submit" class="btn btn-primary btn-small"><?php echo (($game_id > 0)?'Save':'Add');?></button> or <a class="text-danger" href="<?php echo $this->url_index;?>">Cancel</a>
				</div>
		
		</div></div>
		<div id="tab2" class="tab-pane">
		<?php if($game_id>0): ?>
			<div class="form-group">
			<label class="control-label" for="">Game File:: </label>
				<div class="controls">
					<input type="text" id="game_file" name="" class="span3 form-control input-small btn-input-upload" value="<?php echo@$edit->game_file;?>" style="width: 450px;float: left;margin-right: 5px;"> 
					
					 <input type="button" id="game-file" class="btn  btn-primary glyphicon glyphicon-upload upload-game-file" value="Choose file" name="game_file" allowfile="swf">
					 
<span style="padding-left:5px;vertical-align:middle;"><i>SWF (20MB max file size)</i></span>
				<div id="errormsg-game-file" class="clearfix redtext">
		</div>	              
		<div id="pic-progress-wrap-game-file" class="progress-wrap" style="margin-top:10px;margin-bottom:10px;">
		</div>	

		<div id="picbox-game-file" class="clear" style="padding-top:0px;padding-bottom:10px;">
		</div>

				</div>
			</div>
			<?php echo $this->atheme->group_input2('edit[game_file_rate_height_width]', 'Tỉ lệ Height/Width', 'game_rate', 'span6 ', isset($edit->game_file_rate_height_width)?$edit->game_file_rate_height_width:1, 'text', '','Tỉ lệ Height/Width sẽ tự động cập nhật nếu up file mới lên'); ?>
			<?php echo $this->atheme->group_input2('edit[game_video_link]', 'Link youtube hướng dẫn', '', 'span6 ', @$edit->game_video_link, 'text', '','Nhập đường link video hướng dẫn, nếu không có thì để trống'); ?>
			<?php
			if(@$edit->game_video_link != '' && strlen($edit->game_video_link ) ==11)
			{
				?>
				<iframe width="320" height="180" src="//www.youtube.com/embed/<?php echo $edit->game_video_link;?>" frameborder="0" allowfullscreen style="margin-left: 202px;"></iframe>
			<?php
			}
			?>
			<div class="form-group">
				<label class="control-label" for="">Game Thumb Large: </label>
				<div class="controls">
					<img src="<?php echo image_src(@$edit->game_thumb_large);?>" class="btn-input-upload" style="width: 60px;">
					<input type="button" id="game-thumb-large" class="btn  btn-primary glyphicon glyphicon-upload upload-game-file" value="Choose file" name="game_thumb_large"  allowfile="jpg,png,gif">
					<span style="padding-left:5px;vertical-align:middle;"><i>Ảnh lớn của game, chấp nhận file PNG, JPG, or GIF (1MB max file size)</i></span>
					<div id="errormsg-game-thumb-large" class="clearfix redtext">
					</div>	              
					<div id="pic-progress-wrap-game-thumb-large" class="progress-wrap" style="margin-top:10px;margin-bottom:10px;">
					</div>	
					
					<div id="picbox-game-thumb-large" class="clear" style="padding-top:0px;padding-bottom:10px;">
					</div>
					
				</div>
			</div>
			
			<div class="form-group">
			<label class="control-label" for="">Game Thumb:: </label>
				<div class="controls">
					<img src="<?php echo image_src(@$edit->game_thumb);?>" class="btn-input-upload" style="width: 60px;">
					 <input type="button" id="game-thumb" class="btn  btn-primary glyphicon glyphicon-upload upload-game-file" value="Choose file" name="game_thumb"  allowfile="jpg,png">
		<span style="padding-left:5px;vertical-align:middle;"><i>PNG, JPG, or GIF (1MB max file size)</i></span>
		<div id="errormsg-game-thumb" class="clearfix redtext">
		</div>	              
		<div id="pic-progress-wrap-game-thumb" class="progress-wrap" style="margin-top:10px;margin-bottom:10px;">
		</div>	

		<div id="picbox-game-thumb" class="clear" style="padding-top:0px;padding-bottom:10px;">
		</div>

				</div>
			</div>
			
			<?php 
			 else:
			echo "Bạn phải save game rồi mới được thêm thông tin.";
			endif; ?> 
			
		<div class="form-actions">
					<button type="submit" class="btn btn-primary btn-small"><?php echo (($game_id > 0)?'Save':'Add');?></button> or <a class="text-danger" href="<?php echo $this->url_index;?>">Cancel</a>
				</div>
		</div>
		
		
		<style>.select2-container {
float: left;
margin-right: 5px;
}
.select2-container .select2-choice {
min-width: 100px;
height: 31px;
}
.controlsx {
padding: 10px 10px 10px 0;
width: 100%;
clear: both;
margin-bottom: 10px;
}
</style>
		<div id="tab3" class="tab-pane">
 <?php if($game_id>0): ?>
			 
		<div class="widget-content nopadding iframe-key-content">
		<?php 
		 
		for($i=0; $i<count($game_keys); $i++){?>
		<div class="controlsx" id="key-<?php echo $game_keys[$i]->gins_id;?>">
		<?php echo $game_keys[$i]->gins_pre_description;?>
		<?php echo ($game_keys[$i]->gins_key=='NULL')?'':'<img src="'.$this->mgameinstruction->get_image($game_keys[$i]->gins_key).'" title="'.$this->mgameinstruction->key_define[$game_keys[$i]->gins_key].'" />';?>
			<?php echo $game_keys[$i]->gins_description;?>
			 &nbsp;
			</div>
		<?php } ?>
		</div>

		
			
				<?php 
			 else:
			echo "Bạn phải save game rồi mới được thêm thông tin.";
			endif; ?>  
		
		</div> 
		
		
		
		<div id="tabseo" class="tab-pane">
		
<div class="row">
<div class="col-12">
		<div class="widget-content nopadding">
			
			<?php //echo $this->atheme->group_single_upload('userfile', 'Ảnh đại diện', '', ' span6', @$cate->CAT_THUMB, 'text', '80px','80px'); ?>
				<?php echo $this->atheme->group_text2('SEO H1', @$edit->seo_h1); ?>
				<?php echo $this->atheme->group_text2('SEO DESCRIPTION', @$edit->seo_description); ?>
				<?php echo $this->atheme->group_text2('SEO TITLE', @$edit->seo_title); ?>
				<?php echo $this->atheme->group_text2('SEO KEYWORD', @$edit->seo_keyword); ?>
				
				<div class="form-actions">
					<button type="submit" class="btn btn-primary btn-small"><?php echo (($game_id > 0)?'Save':'Add');?></button> or <a class="text-danger" href="<?php echo $this->url_index;?>">Cancel</a>
				</div>
			
		</div>
	 					
 
</div>
				
				</div>
	</div>                            
</div>
</div>

</form>



<script>
$(document).ready(function() {
$(document.body).on('click','.btn-delete-key', function(){
var id = $(this).attr("data-keyid");
var key = $(this).siblings(".input-key-key").find('option:selected').val();

$.ajax({
  type: "POST",
  dataType: "json",
  url: "<?php echo admin_url();?>game/instruction/delete/<?php echo @$game_id;?>",
	data: "key="+key+"&key_id="+id,
	success: function(data){
	if(data.success){
		$("#key-"+id).fadeOut().remove();
		}
		 
	}
	});
});

$(document.body).on('click',".btn-key-update",function(){
var id = $(this).attr("data-keyid");
var key = $(this).siblings(".input-key-key").find('option:selected').val();
var value = $(this).siblings(".input-key-value").val();

$.ajax({
  type: "POST",
  dataType: "json",
  url: "<?php echo admin_url();?>game/instruction/update/<?php echo @$game_id;?>",
	data: "key="+key+"&key_id="+id+"&value="+value,
	success: function(data){
	if(data.success){
		
		}
		 
	}
	});
});

$(".btn-key-add").click(function(){
var key = $("#input-add-key").val();
var value = $(".input-key-value-add").val();
//var value = $(this).attr("input-key-value");
 
 $.ajax({
  type: "POST",
  dataType: "json",
  url: "<?php echo admin_url();?>game/instruction/add_key/<?php echo @$game_id;?>",
	data: "key="+key+"&value="+value,
	success: function(data){
	if(data.success){
		mhtml = '';
		mhtml+= '<div class="controlsx" id="key-'+data.key_id+'">';
		mhtml+= data.html;
		mhtml+= '<input type="text" name="keyval[]" class=" form-control input-small input-key-value" value="'+data.key_value+'" style="width: 200px;float: left;">';
		mhtml+= '<button type="button" class="btn btn-primary btn-small btn-key-update" data-keyid="'+data.key_id+'">Cập nhật</button>';
		mhtml+= '&nbsp;<button type="button" class="btn btn-danger btn-small btn-delete-key" data-keyid="'+data.key_id+'">Xóa</button>';
		mhtml+= '</div>';
		$(".iframe-key-content").append(mhtml);
		$("select.select2").select2();
		$(".input-key-value-add").val("");
		
	}
		 
	}
});
});



function safe_tags( str ) {
  return String( str )
           .replace( /&/g, '&amp;' )
           .replace( /"/g, '&quot;' )
           .replace( /'/g, '&#39;' )
           .replace( /</g, '&lt;' )
           .replace( />/g, '&gt;' );
}

$('.upload-game-file').each(function( index ) {
	txt(this);
});

function txt(mthis){
	var me = $(mthis);
	var str= me.attr("allowfile");
	var btn = me.attr("id"),
	allowfile = str.split(','),
	mname = me.attr("name"),
	wrap = document.getElementById('pic-progress-wrap-'+btn),
	picBox = document.getElementById('picbox-'+btn),
	errBox = document.getElementById('errormsg-'+btn);
  var uploader = new ss.SimpleUpload({
        button: btn,
        url: '<?php echo admin_url();?>game/upload_my_file/<?php echo @$game_id;?>',
        progressUrl: '<?php echo admin_url();?>game/upload_process',
        name: mname,
        multiple: true,
        maxUploads: 1,
        maxSize: 20000,
        allowedExtensions: allowfile,
        accept: '*',
        hoverClass: 'btn-hover',
        focusClass: 'active',
        disabledClass: 'disabled',
        responseType: 'json',
        onExtError: function(filename, extension) {
          alert(filename + ' is not a permitted file type.'+"\n\n"+'Only SWF files are allowed.');
        },
        onSizeError: function(filename, fileSize) {
          alert(filename + ' is too big. (20MB max file size)');
        },        
        onSubmit: function(filename, ext) {            
           var prog = document.createElement('div'),
               outer = document.createElement('div'),
               bar = document.createElement('div'),
               size = document.createElement('div');
                       
            prog.className = 'prog';
            size.className = 'size';
            outer.className = 'progress progress-striped active';
            bar.className = 'progress-bar progress-bar-success';
            
            outer.appendChild(bar);
            prog.innerHTML = '<span style="vertical-align:middle;">'+safe_tags(filename)+' - </span>';
            prog.appendChild(size);
            prog.appendChild(outer);
            wrap.appendChild(prog); // 'wrap' is an element on the page
            
            this.setProgressBar(bar);
            this.setProgressContainer(prog);
            this.setFileSizeBox(size);      
            
            errBox.innerHTML = '';
            btn.value = 'Choose another file';
          },		
        startXHR: function() {
          // Dynamically add a "Cancel" button to be displayed when upload begins
          // By doing it here ensures that it will only be added in browses which 
          // support cancelling uploads
          var abort = document.createElement('button');
            
            wrap.appendChild(abort);
            abort.className = 'btn btn-sm btn-info';
            abort.innerHTML = 'Cancel';

            // Adds click event listener that will cancel the upload
            // The second argument is whether the button should be removed after the upload
            // true = yes, remove abort button after upload
            // false/default = do not remove
            this.setAbortBtn(abort, true);              
        },          
        onComplete: function(filename, response) {
            if (!response) {
              errBox.innerHTML = 'Unable to upload file';
              return;
            }     
            if (response.success === true) {
              picBox.innerHTML = '<div class="alert alert-success"><button class="close" data-dismiss="alert">×</button><strong>Success! </strong> The file upload success.</div>';
              $(me).siblings(".btn-input-upload").attr("src",response.file);
              $(me).siblings(".btn-input-upload").val(response.file);
			  if(response.rate)
				$("#game_rate").val(response.rate);
			  console.log(response);
            } else {
              if (response.msg)  {
                errBox.innerHTML = '<div class="alert alert-danger"><button class="close" data-dismiss="alert">×</button><strong>Error! </strong>'+response.msg+'</div>';
              } else {
                errBox.innerHTML = 'Unable to upload file';
              }
            }
          }
	});
//});
};

});
</script>